package com.xbongbong.callback.aspect;

import com.xbongbong.callback.help.ApiCallbackHelper;
import com.xbongbong.paas.toolbox.wrap.EnableApiCallBack;
import com.xbongbong.pro.enums.ApiCallBackEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.saas.enums.OperateTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Method;

/**
 * API-WebHook回调切面处理
 * @author 李少杰
 * @version v1.0.0
 * @date 2021/8/27 09:52
 */
@Aspect
@Component
public class ApiCallBackAspect {
    private static final Logger LOG = LoggerFactory.getLogger(ApiCallBackAspect.class);
    @Resource
    private ApiCallbackHelper apiCallbackHelper;
    @Pointcut("@annotation(com.xbongbong.paas.toolbox.wrap.EnableApiCallBack)")
    public void pointCut(){}


    @AfterReturning("pointCut()")
    public void afterReturn(JoinPoint joinPoint) {
        try {
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();
            Object[] param = joinPoint.getArgs();
            EnableApiCallBack enableApiCallBack = method.getAnnotation(EnableApiCallBack.class);
            ApiCallBackEnum apiCallBackEnum = enableApiCallBack.callBackBusiness();
            XbbRefTypeEnum xbbRefTypeEnum = apiCallBackEnum.getXbbRefTypeEnum();
            SaasMarkEnum saasMarkEnum = enableApiCallBack.saasMark();
            OperateTypeEnum operateTypeEnum = enableApiCallBack.operateTypeEnum();
            boolean isBatch = enableApiCallBack.isBatch();
            apiCallbackHelper.callBackByAspect(xbbRefTypeEnum, saasMarkEnum, operateTypeEnum, param, isBatch);
        } catch (Throwable throwable) {
            LOG.error("ApiCallBack error: ", throwable);
        }
    }
}
